//
//  DirectShowCamViewController.m
//  DirectShowCam
//
//  Created by Evangel on 10-12-28.
//  Copyright 2010 Evangel. All rights reserved.
//

#import "DirectShowCamViewController.h"

@implementation DirectShowCamViewController
@synthesize currentSourceType;

//This just for test. cut image
-(UIImage*)cropImageWithInputImage:(UIImage*)inImage
{
	CGImageRef imgRef = inImage.CGImage;
	CGFloat width = CGImageGetWidth(imgRef);
	CGFloat height = CGImageGetHeight(imgRef);
	
	UIImage *newImage = nil;
	if(width >=320.0 && height >= 320.0)
	{
		CGRect newR = CGRectMake(0.0, 44.0, 320.0, 320.0); //You can change this logic
		CGImageRef finalImgRef = CGImageCreateWithImageInRect(imgRef,newR);
		newImage = [UIImage imageWithCGImage:finalImgRef];
	}
	//NSLog(@"2w:%f,2h:%f",newImage.size.width,newImage.size.height);
	return newImage;
}

- (CGSize)eventDetailFitSize:(CGSize)thisSize inSize: (CGSize) aSize
{
	//aSize: size that resize to
	//thisSize: orign size
	CGFloat scale;
	CGSize newsize;
	
	if(thisSize.width<= aSize.width && thisSize.height <= aSize.height)
	{
		newsize = thisSize;
	}
	else 
	{
		if(thisSize.width >= thisSize.height)
		{
			scale = aSize.width/thisSize.width;
			newsize.width = aSize.width;
			newsize.height = thisSize.height*scale;
		}
		else 
		{
			scale = aSize.height/thisSize.height;
			newsize.height = aSize.height;
			newsize.width = thisSize.width*scale;
		}
	}
	return newsize;
}

- (UIImage *)eventDetailImage:(UIImage *) image fitInSize:(CGSize)viewsize
{
	CGSize size = [self eventDetailFitSize:image.size	 inSize:viewsize];
	UIGraphicsBeginImageContext(size);
	
	CGRect rect = CGRectMake(0, 0, size.width, size.height);
	[image drawInRect:rect];
	
	UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();  
	
	return newimg;  
}

#pragma mark -
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
	if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
	{
		albumFromCamera = NO;
		callingRecord = 0;
	}
	return self;
}

- (void)viewDidLoad 
{	
	if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
	{
		[self showCamera];
	}
	else 
	{
		[self showAlbum];
	}
}

- (void)viewDidUnload 
{
	[super viewDidUnload];
}

- (void)dealloc 
{
	[imagePickerController release];
	[albumPicker release];
	[super dealloc];
}

#pragma mark -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
	NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
	UIImage *image = nil;
	if ([mediaType isEqualToString:@"public.image"])
	{
		image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
		//NSLog(@"w:%f,h:%f",image.size.width,image.size.height);
	}
	if(currentSourceType == UIImagePickerControllerSourceTypeCamera)
	{
		self.wantsFullScreenLayout = NO;
		picker.wantsFullScreenLayout = NO;
		[[UIApplication sharedApplication] setStatusBarHidden:NO];
	}
	[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
	
	[picker dismissModalViewControllerAnimated:NO];
	[self dismissModalViewControllerAnimated:NO];
	
	//push vc
//	DirectShowCamAppDelegate *app = (DirectShowCamAppDelegate*)[UIApplication sharedApplication].delegate;
	OtherController *oC = [[OtherController alloc] initWithNibName:@"OtherController" bundle:nil];
	oC.delegate = self;
	oC.passInType = currentSourceType;
	//oC.currentImage = [self cropImageWithInputImage:image];
	oC.currentImage = [self eventDetailImage:image fitInSize:CGSizeMake(320.0, 480.0)];
	[self.navigationController pushViewController:oC animated:YES];
	[oC release];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
	if(albumFromCamera && callingRecord == 2)
	{
		albumFromCamera = NO;
		[imagePickerController dismissModalViewControllerAnimated:YES];
		currentSourceType = UIImagePickerControllerSourceTypeCamera;
	}
	else 
	{
		[self dismissModalViewControllerAnimated:YES];
		currentSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	}
	callingRecord--;
}

#pragma mark -
- (UIView*) pickerAttachView 
{
	UIView* attachview = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
	
	UIImageView *bgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 424, 320, 56)];
	bgview.image = [UIImage imageNamed:@"bottombarback.png"];
	[attachview addSubview:bgview];
	[bgview release];
	
	UIButton* filebtn = [UIButton buttonWithType:UIButtonTypeCustom];
	filebtn.backgroundColor = [UIColor clearColor];
	filebtn.frame = CGRectMake(18, 430, 42, 42);
	[filebtn setImage:[UIImage imageNamed:@"filenormal.png"] forState:UIControlStateNormal];
	[filebtn addTarget:self action:@selector(closeCameraAndShowAlbum) forControlEvents:UIControlEventTouchUpInside];
	[attachview addSubview:filebtn];
	
	UIButton* takebtn = [UIButton buttonWithType:UIButtonTypeCustom];
	takebtn.backgroundColor = [UIColor clearColor];
	takebtn.frame = CGRectMake(139, 430, 42, 42);
	[takebtn setImage:[UIImage imageNamed:@"cameranormal.png"] forState:UIControlStateNormal];
	[takebtn addTarget:self action:@selector(capture) forControlEvents:UIControlEventTouchUpInside];
	[attachview addSubview:takebtn];
	
	UIButton* closebtn = [UIButton buttonWithType:UIButtonTypeCustom];
	closebtn.backgroundColor = [UIColor clearColor];
	closebtn.frame = CGRectMake(260, 430, 42, 42);
	[closebtn setImage:[UIImage imageNamed:@"closenormal.png"] forState:UIControlStateNormal];
	[closebtn addTarget:self action:@selector(terminateImageTakeController) forControlEvents:UIControlEventTouchUpInside];
	[attachview addSubview:closebtn];	
	
	return attachview;
}

- (void)closeCameraAndShowAlbum 
{
	albumFromCamera = YES;
	callingRecord ++;

	if (albumPicker == nil) 
	{
		albumPicker = [[UIImagePickerController alloc] init];
		albumPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
		albumPicker.delegate = self;
	}
	
	[[UIApplication sharedApplication] setStatusBarHidden:NO];
	
	currentSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	[imagePickerController presentModalViewController:albumPicker animated:YES];
}

//call this method only from camera mode
- (void)terminateImageTakeController 
{
	[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
	
	[self dismissModalViewControllerAnimated:YES];
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)showCamera 
{
	if (imagePickerController == nil) 
	{
		imagePickerController = [[UIImagePickerController alloc] init];		
		imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
		imagePickerController.showsCameraControls = NO;
		imagePickerController.allowsEditing = NO;
		imagePickerController.cameraOverlayView = [self pickerAttachView];
		imagePickerController.delegate = self;
	}
	if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
	{
		[self presentModalViewController:imagePickerController animated:NO];
		currentSourceType = UIImagePickerControllerSourceTypeCamera;
		callingRecord++;
	}
}

- (void)showAlbum 
{
	if (albumPicker == nil) 
	{
		albumPicker = [[UIImagePickerController alloc] init];
		albumPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
		albumPicker.delegate = self;
		albumPicker.allowsEditing = NO;
	}
	[self presentModalViewController:albumPicker animated:NO];
	currentSourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	callingRecord ++;
}

- (void) capture 
{
	[imagePickerController takePicture];
}

#pragma mark -
-(void)returnToCamState:(UIImagePickerControllerSourceType)type
{
	if(albumFromCamera && callingRecord == 2)
	{
		albumFromCamera = NO;
		callingRecord = 0;
		
		[self showCamera];
		[self closeCameraAndShowAlbum];
	}
	else if(type == UIImagePickerControllerSourceTypeCamera)
	{
		callingRecord = 0;
		[self showCamera];
	}
	else if(type == UIImagePickerControllerSourceTypePhotoLibrary)
	{
		callingRecord = 0;
		[self showAlbum];
	}
	else 
	{
		NSLog(@"not rigth way");
	}
}
@end
